package com.sdry.web.controller.approvalManagement;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.entity.ApprovalEntity;
import com.sdry.entity.approvalManagement.ApprovalDetail;
import com.sdry.entity.approvalManagement.ModelEntity;
import com.sdry.service.ApprovalInitiateService;
import com.sdry.service.approval.ApprovalService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.RootPath;

/**
 * @ClassName: ApprovalInitiateController 
 * @Description:发起审批Controller
 * @Author hpf
 * @Date 2018年8月30日 下午3:45:37 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/initiateMana")
public class ApprovalInitiateController {

	@Resource
	private ApprovalInitiateService approvalInitiateService;
	@Resource
	private ApprovalService approvalService;
	
	/**
	 * 查询所有审批模板
	 * @return
	 */
	@RequestMapping(value="/listAllModel")
	public ModelAndView listAllModel(){
		ModelAndView mv = new ModelAndView();
		List<ModelEntity> modelList = approvalInitiateService.listAllModel();
		mv.addObject("modelList", modelList);
		mv.setViewName("/approval/sponsor");
		return mv;
	}
	
	/**
	 * 根据ID查询审批模板
	 * @param mId
	 * @return
	 */
	@RequestMapping(value="/findModelByID")
	public ModelAndView findModelByID(long mId){
		ModelAndView mv = new ModelAndView();
		ModelEntity modelEntity = approvalInitiateService.findModelByID(mId);
		Map<String, String> map = new LinkedHashMap<String, String>();
		//List<String> mFieldList = new ArrayList<>();
		String[] mFieldArr = modelEntity.getmFields().split(", ");
		String[] mFieldTypeArr = modelEntity.getRemark01().split(", ");
		for (int i = 0; i < mFieldArr.length; i++) {
			map.put(mFieldArr[i], mFieldTypeArr[i]);
			//mFieldList.add(mFieldArr[i]);
		}
		//modelEntity.setmFieldList(mFieldList);
		mv.addObject("model1", modelEntity);
		mv.addObject("model", map);
		mv.setViewName("/approval/approvalInitiate");
		return mv;
	}
	
	/**
	 * 根据审批ID查询详情
	 * @param approvalId
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="findDetailByID")
	public void findDetailByID(long approvalId,HttpServletResponse response) throws Exception{
		ApprovalDetail approvalDetail = approvalInitiateService.findDetailByID(approvalId);
		JSONObject json = JSONObject.fromObject(approvalDetail);
        ResponseUtil.write(response, json);
	}
	
	/**
	 * 附件修改
	 * @param approvalEntity
	 * @param attachment
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/updateApproval")
	public void updateApproval(ApprovalEntity approvalEntity,@RequestParam("attachment") MultipartFile attachment,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String uploadURL = RootPath.getRootPath("NATIVE_PATH");
		String oldName = approvalService.findApprovalEntityByID(approvalEntity.getApprovalId()).getRemark06();
		String suffix = oldName.substring(oldName.lastIndexOf("."));
		String fileName = oldName.substring(0,oldName.lastIndexOf("."));
		String name = fileName.substring(0, fileName.lastIndexOf("_"));
		String oriFileName = name + "_" + sdf.format(new Date()) + suffix;
        String path = uploadURL + oriFileName;
		File newFile = new File(path);
		//通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        attachment.transferTo(newFile);
		/*_________________________________________*/
		if (attachment.equals(null)) {
			approvalEntity.setRemark06(null);
		} else {
			approvalEntity.setRemark06(oriFileName);
		}
		isSuccess = approvalInitiateService.updateApproval(approvalEntity);
		if (isSuccess > 0) {
			ResponseUtil.write(response, isSuccess);
		} else {
			ResponseUtil.write(response, 0);
		}
	}
}
